import logging
from logging import debug
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

import tables as tb
import numpy as np

def generate_time_seq():
    from dateutil import rrule
    import datetime
    d1 = datetime.datetime(2011, 01, 01, 0)
    d2 = datetime.datetime(2012, 01, 01, 0)
    list(rrule.rrule(rrule.MINUTELY, interval=10, dtstart=d1, until=d2))

def write_to_h5(data, file_path):
    name_list = data.dtype.names

    import particle as db_cl

    filters = tb.Filters(complevel=2, complib="blosc")
    h5file = tb.openFile(file_path, mode='a', title="SCADA Data", filters=filters)

    group = h5file.root

    if "data" in h5file.root._v_children.keys():
        table = h5file.root._v_children["data"]
    else:
        table = h5file.createTable(group, "data", db_cl.Particle)
    #name_data = zip(name_list, data)
    #name_data.sort()
    #names, data = zip(*name_data)
    #print len(data[0])
    #print len(names)

    # table append does not work when there is string in the data :S thus looping through the data
    # table.append([data[:]])
    particle = table.row
    for dat in data:
        for name in name_list:
            particle[name] = dat[name]
        particle.append()
    table.flush()

    h5file.close()

